#include "trap.h"
#include "ysyxsoc.h"
#include "riscv.h"

// 0xa0000000 ~ 0xa4000000
void uint8_test (size_t addr_base,size_t len){
    for (size_t i = addr_base; i < addr_base+len; i++)
    {
        outb(i, (uint8_t)(i & 0x00000000ff));
    }
    for (size_t i = addr_base; i < addr_base+len; i++)
    {
        check(inb(i) == (uint8_t)(i & 0x00000000ff));
    }
}


void uint16_test(size_t addr_base, size_t len)
{
    for (size_t i = addr_base; i < addr_base + len; i = i + 2)
    {
        outw(i, (uint16_t)(i & 0x000000ffff));
    }
    for (size_t i = addr_base; i < addr_base + len; i = i + 2)
    {
        check(inw(i) == (uint16_t)(i & 0x000000ffff));
    }
}

void uint32_test(size_t addr_base, size_t len)
{
    for (size_t i = addr_base; i < addr_base + len; i = i + 4)
    {
        outl(i, (uint32_t)(i & 0xffffffff));
    }
    for (size_t i = addr_base; i < addr_base + len; i = i + 4)
    {
        check(inl(i) == (uint32_t)(i & 0xffffffff));
    }
}
void uint64_test(size_t addr_base, size_t len)
{
    for (size_t i = addr_base; i < addr_base + len; i = i + 8)
    {
        outd(i, (uint64_t)((uint64_t)i & 0xffffffffffffffff));
    }
    for (size_t i = addr_base; i < addr_base + len; i = i + 8)
    {
        check(ind(i) == (uint64_t)((uint64_t)i & 0xffffffffffffffff));
    }
}

// 0xa0000000 ~ 0xa8000000
void unalign_test(size_t addr_base, size_t len)
{
    for (size_t i = addr_base; i < addr_base + len; i = i + 7)
    {
        outb(i, (uint8_t)(i & 0x00000000ff));
        outw(i + 1, (uint16_t)((i + 1) & 0x000000ffff));
        outl(i + 3, (uint32_t)((i + 3) & 0x00ffffffff));
        check(inb(i) == (uint8_t)(i & 0x00000000ff));
        check(inw(i + 1) == (uint16_t)((i + 1) & 0x000000ffff));
        check(inl(i + 3) == (uint32_t)((i + 3) & 0x00ffffffff));
    }
    for (size_t i = addr_base; i < addr_base + len; i = i + 7)
    {
        check(inb(i) == (uint8_t)(i & 0x00000000ff));
        check(inw(i + 1) == (uint16_t)((i + 1) & 0x000000ffff));
        check(inl(i + 3) == (uint32_t)((i + 3) & 0x00ffffffff));
    }
}

int main() {
    uint8_test(PSRAM_ADDR+PSRAM_SIZE / 8 + 0x1, 2048);

    // uint16_test(PSRAM_ADDR + PSRAM_SIZE/4, PSRAM_SIZE/4);
    // uint32_test(PSRAM_ADDR + PSRAM_SIZE/2, PSRAM_SIZE/4);
    // unalign_test(PSRAM_ADDR + PSRAM_SIZE / 2 + PSRAM_SIZE / 4, PSRAM_SIZE/4);
    return 0;
}
